package com.aaa.service.impl;

import com.aaa.common.domain.Role;
import com.aaa.common.query.RoleQuery;
import com.aaa.common.result.Result;
import com.aaa.mapper.RoleMapper;
import com.aaa.service.RoleService;
import com.aaa.util.GFSStringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {


    @Autowired
    private RoleMapper mapper;

    /**
     * 分页查询角色表
     * @param roleQuery
     * @return
     */
    @Override
    public Result listRole(RoleQuery roleQuery) {
        Page<Role> page = new Page<>(roleQuery.getPage(),roleQuery.getCount());
        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.eq("status",1);
        qw.like(GFSStringUtil.StrNotNull(roleQuery.getRoleName()),"role_name",roleQuery.getRoleName());
        Page<Role> rolePage = mapper.selectPage(page, qw);
        return Result.success(rolePage);
    }

    /**
     * 角色表添加
     * @param role
     * @return
     */
    @Override
    public boolean addRole(Role role) {
        int insert = mapper.insert(role);
        if (insert>0){
            return true;
        }
        return false;
    }

    /**
     * 查询已选中的复选框
     * @return
     */
    @Override
    public Result queryRoleCheckbox() {
        //查询角色表
        List<Role> roles = mapper.queryRole();
        return Result.success(roles);
    }

    /**
     * 通过id查询用户角色表数据
     * @return
     */
    @Override
    public Result listRoleById(Integer id) {
        List<Role> roles = mapper.queryRoleById(id);
        return Result.success(roles);
    }

    /**
     * 角色表修改
     * @param role
     * @return
     */
    @Override
    public boolean updateRole(Role role) {
        int updateById = mapper.updateById(role);
        if (updateById>0){
            return true;
        }
        return false;
    }

    /**
     * 角色表删除
     * @param ids
     * @return
     */
    @Override
    public boolean deleteRole(String ids) {
        //将前端传过来的id字符串转换成list集合
        String[] split = ids.split(",");
        List<String> strings = Arrays.asList(split);
        boolean delete = mapper.deleteRole(strings);
        if (delete==true){
            return true;
        }
        return false;
    }
}
